home *** CD-ROM | disk | FTP | other *** search
- Path: newsfeed.direct.ca!usenet
- From: qjackson@direct.ca
- Newsgroups: comp.lang.c++
- Subject: Re: How to handle error in constructor
- Date: Sat, 03 Feb 1996 16:03:59 GMT
- Organization: Parsepolis Software
- Message-ID: <4f00u4$s5n@orb.direct.ca>
- References: <DLyyIM.5EG@teslab.lab.oz.au>
- Reply-To: qjackson@direct.ca
- NNTP-Posting-Host: 204.174.245.169
- X-Newsreader: Forte Free Agent 1.0.82
-
- andrew@teslab.lab.oz.au (Andrew Phillips) wrote:
-
- >I'm converting and enhancing a simple program in C to C++. Without going
- >into too much detail I have a class that represents a file on disk. The
- >contructor opens the file, but what should it do if the file cannot be
- >opened? The C program just calls fopen() tests the return value and
- >if there's an error it prints a message and exits.
-
- >In C++ I thought to set a flag in the constructor and have a member function
- >that tests the flag to see if the file was opened correctly. This seems
- >rather inelegant -- I guess exceptions would be the elegant way but seem
- >like overkill for such a simple program. I've looked in several C++ books but
- >error handling is given scant coverage except for exception handling.
-
- >Any suggestions would be greatly appreciated.
-
- Hello, Andrew!
-
- I am currently designing a class (or rather, a hierarchy of classes
- that act as one class in the end)that parses a string as part of the
- construction process. I can't throw exceptions because the version of
- the Turbo C++ compiler that I am using (3.00) doesn't have exception
- handling. What's more, I'm not sure I would want to throw and catch
- even if I had the ball and glove.
-
- Since construction of an object of the LpmRule class involves about 15
- subprocesses, any number of errors can occur during the construction
- of such an object. I have considered this eventuality and pass an
- explicit ErrorCode parameter to each sub-function under the
- constructor. The construction itself reads, in part:
-
- LpmTokenizer::LpmTokenizer
- (
- shuString& theRule
- )
- {
-
- StateType ErrorCode;
- STR_LENGTH ErrorPtr;
-
- parse
- (
- theRule_,
- "",
- FALSE,
- ErrorCode,
- ErrorPtr
- );
-
- if (ErrorCode)
- {
- errorCleanUp(); // this function detaches all objects attached
- // to this class by pointers using 'delete'
-
- lastError_ = ErrorCode;
- errorPtr_ = ErrorPtr;
- }
- else
- {
- lastError_ = (StateType)0;
- errorPtr_ = (STR_LENGTH)0;
- }
-
- }
-
- //////
-
-
- Suppose I construct an LpmTokenizer object with a bad string:
-
- LpmTokenizer myRule = "[['this is bad becase there are two [s'$";
-
- If I'm not sure that it will parse correctly, I check like this:
-
- if (myRule.lastError() != STATE_NO_ERROR)
- {
- cout << "Rule didn't parse properly!" << "\n";
- exit(myRule.lastError());
- }
-
- I realize this is not in keeping with throwing and catching
- methodology, but as I said, the compiler under which I am developing
- this class does not have exception handling.
-
- Moreover, since LpmRule objects may be created to and fro in string
- comparison expressions on the fly, I did it this way rather than using
- an 'init' member function in order to allow temporary casts using the
- following syntax:
-
- if ((LpmRule)"['C';'C++'$" == "C")
- {
- cout << "String is either \"C\" or \"C++\"" << "\n";
- }
-
- This type of thing wouldn't be as easily expressed if I had to
- construct and then initialize as separate steps:
-
- LpmRule myRule;
- myRule.init("['C';'C++'$");
-
- if (myRule == "C")
- {
- cout << "String is either \"C\" or \"C++\"" << "\n";
- }
-
- (The first method eats up less name space, since anonymous objects can
- be created if they're needed for quick comparisons.)
-
- The only caution is that you must be sure to check your error flag
- before doing anything critical with such an object, if the constructor
- can fail. In the case of LpmRule -- it is assumed that any "hard
- coded" rule will be known to parse properly, whereas any user supplied
- rule will not be known to parse. The programmer will be able to take
- advantage of this fact and will therefore only have to do explicit
- assertion checking when the object is being constructed with an
- unknown string. (Although changes in the underlying syntax still make
- it prudent to do explicit assertion checks even if the hardcoded rules
- are known to parse.)
-
-
- Cheers,
-
-
-
-
- --
- |
- Parsepolis Software | Quinn Tyler Jackson
- "ParseCity" | (aka 'Jamshid')
- >--------------------------| qjackson@direct.ca
- |---------------------->
-
-